/*
 * @Author: 张勇 1179276268@qq.com
 * @Date: 2024-01-05 11:22:56
 * @LastEditors: 张勇 1179276268@qq.com
 * @LastEditTime: 2024-01-05 17:24:31
 * @FilePath: \project-template\src\utils\tool.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 通用方法

//   // 回显数据字典
export function selectDictLabel(datas: any, value: string) {
  if (value === undefined) {
    return ''
  }
  const actions = []
  Object.keys(datas).some((key) => {
    if (datas[key].value == '' + value) {
      actions.push(datas[key].label)
      return true
    }
  })
  if (actions.length === 0) {
    actions.push(value)
  }
  return actions.join('')
}

//   // 回显数据字典（字符串数组）
export function selectDictLabels(datas: any, value: string, separator: string) {
  if (value === undefined || value.length === 0) {
    return ''
  }
  if (Array.isArray(value)) {
    value = value.join(',')
  }
  const actions: any[] = []
  const currentSeparator = undefined === separator ? ',' : separator
  const temp = value.split(currentSeparator)
  Object.keys(value.split(currentSeparator)).some((val: any) => {
    let match = false
    Object.keys(datas).some((key) => {
      if (datas[key].value == '' + temp[val]) {
        actions.push(datas[key].label + currentSeparator)
        match = true
      }
    })
    if (!match) {
      actions.push(temp[val] + currentSeparator)
    }
  })
  return actions.join('').substring(0, actions.join('').length - 1)
}

// // 字符串格式化(%s )
// export function sprintf(str: string):string {
//   const args:IArguments = arguments,
//     flag = true,
//     i = 1
//   str = str.replace(/%s/g, function () {
//     var arg = args[i++]
//     if (typeof arg === 'undefined') {
//       flag = false
//       return ''
//     }
//     return arg
//   })
//   return flag ? str : ''
// }

// 转换字符串，undefined,null等转化为""
export function parseStrEmpty(str: string) {
  if (!str || str == 'undefined' || str == 'null') {
    return ''
  }
  return str
}

// 数据合并
export function mergeRecursive(source: any, target: any) {
  for (const p in target) {
    try {
      if (target[p].constructor == Object) {
        source[p] = mergeRecursive(source[p], target[p])
      } else {
        source[p] = target[p]
      }
    } catch (e) {
      source[p] = target[p]
    }
  }
  return source
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
// export function handleTree(data:any, id:string, parentId:string, children:any) {
//   const config = {
//     id: id || 'id',
//     parentId: parentId || 'parentId',
//     childrenList: children || 'children'
//   }

//   const childrenListMap = {}
//   const nodeIds = {}
//   const tree = []

//   for (const d of data) {
//     const parentId = d[config.parentId]
//     if (childrenListMap[parentId] == null) {
//       childrenListMap[parentId] = []
//     }
//     nodeIds[d[config.id]] = d
//     childrenListMap[parentId].push(d)
//   }

//   for (let d of data) {
//     let parentId = d[config.parentId]
//     if (nodeIds[parentId] == null) {
//       tree.push(d)
//     }
//   }

//   for (let t of tree) {
//     adaptToChildrenList(t)
//   }

//   function adaptToChildrenList(o) {
//     if (childrenListMap[o[config.id]] !== null) {
//       o[config.childrenList] = childrenListMap[o[config.id]]
//     }
//     if (o[config.childrenList]) {
//       for (let c of o[config.childrenList]) {
//         adaptToChildrenList(c)
//       }
//     }
//   }
//   return tree
// }

/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params: any): string {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    const part = encodeURIComponent(propName) + '='
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
            const params = propName + '[' + key + ']'
            const subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  return result
}

// 返回项目路径
export function getNormalPath(p: string) {
  if (p.length === 0 || !p || p == 'undefined') {
    return p
  }
  const res = p.replace('//', '/')
  if (res[res.length - 1] === '/') {
    return res.slice(0, res.length - 1)
  }
  return res
}

// 验证是否为blob格式
export function blobValidate(data: Blob) {
  return data.type !== 'application/json'
}
